import { isEmpty, forEach } from 'lodash'

/**
 * 树节点找到某个节点
 * @param root tree节点 ObjectType[]
 * @param params ObjectType
 * @returns ObjectType
 */
export function findNode(root, params) {
  const stack = [...root]
  const key = Object.keys(params)[0]
  let node
  stack.push(root)
  while (stack.length > 0) {
    node = stack.pop();
    if (node[key] == params[key]) {
      return node;
    } else if (!isEmpty(node.children)) {
      forEach(node.children, (item) => stack.push(item))
    }
  }
  return {};
}
